import 'dart:convert';
import 'dart:ui';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../base/Constant.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_aygsh/http/Api.dart';
import '../../util/AppSaveInfoUtil.dart';
import '../../util/SignUtil.dart';
import '../../base/application.dart';
import '../../http/HttpUtil.dart';

class MyEarnPage extends StatefulWidget {
  MyEarnPage({Key key}) : super(key: key);

  @override
  _MyEarnPageState createState() => _MyEarnPageState();
}

class _MyEarnPageState extends State<MyEarnPage> {
    String token = "";
    String num_today = "0";
      String  num_month = "0";
        String commission_all = '0';

      String  commission_wait_today= '0';
      String  commission_wait_yesterday= '0';
      String  commission_all_month= '0';
      String  commission_finish_month= '0';
      String  commission_all_lastmonth= '0';
      String  commission_finish_lastmonth= '0';

  EasyRefreshController _controller = EasyRefreshController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: Text('我的收益',
            style: TextStyle(
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                color: Colors.white)),
        backgroundColor: Colors.red,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.white),
        elevation: 0,
      ),
      body: new EasyRefresh(
          enableControlFinishRefresh: true,
          enableControlFinishLoad: true,
          controller: _controller,
          header: MaterialHeader(),
          child: Container(
              child: CustomScrollView(shrinkWrap: true, slivers: <Widget>[
            SliverToBoxAdapter(
                child: Container(
                    padding: EdgeInsets.only(
                        top: ScreenUtil.getInstance().setHeight(50)),
                    alignment: Alignment.center,
                    height: ScreenUtil.getInstance().setHeight(200),
                    color: Colors.red,
                    child: Column(children: <Widget>[
                      Container(
                          child: Text('我通过日尚臻选赚了',
                              style: TextStyle(
                                  color: white,
                                  fontSize:
                                      ScreenUtil.getInstance().setSp(30)))),
                      Container(
                        height: 5,
                      ),
                      Container(
                          child: Text(commission_all,
                              style: TextStyle(
                                  color: white,
                                  fontSize:
                                      ScreenUtil.getInstance().setSp(40))))
                    ]))),
            SliverToBoxAdapter(
                child: Container(
                    alignment: Alignment.centerLeft,
                    margin: EdgeInsets.all(10),
                    child: Column(children: <Widget>[
                      Container(
                          alignment: Alignment.centerLeft,
                          child: Text('日估收益',
                              style: TextStyle(
                                  color: black,
                                  fontSize: ScreenUtil.getInstance().setSp(30),
                                  fontWeight: FontWeight.w500))),
                      Container(
                          alignment: Alignment.center,
                          // margin: EdgeInsets.all(10),
                          padding: EdgeInsets.all(15),
                          color: white,
                          child: Row(
                            children: <Widget>[
                              Expanded(
                                child: Column(children: <Widget>[
                                  Text('今日订单数',
                                      style: TextStyle(
                                        color: black_99,
                                        fontSize:
                                            ScreenUtil.getInstance().setSp(27),
                                      )),
                                  Container(
                                    height: 5,
                                  ),
                                  Text(num_today,
                                      style: TextStyle(
                                        color: Colors.red,
                                        fontSize:
                                            ScreenUtil.getInstance().setSp(35),
                                      )),
                                ]),
                              ),
                              Expanded(
                                  child: Column(children: <Widget>[
                                Text('今日预估收益',
                                    style: TextStyle(
                                      color: black_99,
                                      fontSize:
                                          ScreenUtil.getInstance().setSp(27),
                                    )),
                                Container(
                                  height: 5,
                                ),
                                Text(commission_wait_today,
                                    style: TextStyle(
                                      color: Colors.red,
                                      fontSize:
                                          ScreenUtil.getInstance().setSp(35),
                                    )),
                              ])),
                              Expanded(
                                  child: Column(children: <Widget>[
                                Text('昨日预估收益',
                                    style: TextStyle(
                                      color: black_99,
                                      fontSize:
                                          ScreenUtil.getInstance().setSp(27),
                                    )),
                                Container(
                                  height: 5,
                                ),
                                Text(commission_wait_yesterday,
                                    style: TextStyle(
                                      color: Colors.red,
                                      fontSize:
                                          ScreenUtil.getInstance().setSp(35),
                                    )),
                              ]))
                            ],
                          )),
                      Container(
                          alignment: Alignment.centerLeft,
                          child: Text('月估收益',
                              style: TextStyle(
                                  color: black,
                                  fontSize: ScreenUtil.getInstance().setSp(30),
                                  fontWeight: FontWeight.w500))),
                      Container(
                          alignment: Alignment.bottomLeft,
                          // margin: EdgeInsets.all(10),
                          padding: EdgeInsets.all(15),
                          color: white,
                          child: Column(
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Expanded(
                                    child: Column(children: <Widget>[
                                      Text('本月预估收益',
                                          style: TextStyle(
                                            color: black_99,
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(27),
                                          )),
                                      Container(
                                        height: 5,
                                      ),
                                      Text(commission_all_month,
                                          style: TextStyle(
                                            color: Colors.red,
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(35),
                                          )),
                                    ]),
                                  ),
                                  Expanded(
                                      child: Column(children: <Widget>[
                                    Text('本月已结算收益',
                                        style: TextStyle(
                                          color: black_99,
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(27),
                                        )),
                                    Container(
                                      height: 5,
                                    ),
                                    Text(commission_finish_month,
                                        style: TextStyle(
                                          color: Colors.red,
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(35),
                                        )),
                                  ])),
                                ],
                              ),
                              Container(
                                height: 5,
                              ),
                              Row(
                                children: <Widget>[
                                  Expanded(
                                    child: Column(children: <Widget>[
                                      Text('上月预估成交',
                                          style: TextStyle(
                                            color: black_99,
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(27),
                                          )),
                                      Container(
                                        height: 5,
                                      ),
                                      Text(commission_all_lastmonth,
                                          style: TextStyle(
                                            color: Colors.red,
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(35),
                                          )),
                                    ]),
                                  ),
                                  Expanded(
                                      child: Column(children: <Widget>[
                                    Text('上月已结算收益',
                                        style: TextStyle(
                                          color: black_99,
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(27),
                                        )),
                                    Container(
                                      height: 5,
                                    ),
                                    Text(commission_finish_lastmonth,
                                        style: TextStyle(
                                          color: Colors.red,
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(35),
                                        )),
                                  ])),
                                ],
                              )
                            ],
                          )),
                    ])))
          ]))),
    );
  }

  @override
  void initState() {
    super.initState();
    getString(TOKEN).then((value) {
      if (value == null) {
        Navigator.of(context).pushNamed('login');
      } else {
        token = value;
        getStatistics();
      }
    });
  }

  getStatistics() async {
    Map map = new Map();
    map['token'] = token;
    map['sign_type'] = createSignType(Api.URL_USER_Statistics);
    // print(map);
    Response<String> response =
        await HttpUtil().post(Api.URL_USER_Statistics, data: getSign(map));
    Map userMap = json.decode(response.data);
    print('=========统计数据======');
    print(userMap);
    if (userMap['code'] == 1 && mounted) {
      setState(() {
        num_today = userMap['data']['orderStatistics']['num_today'].toString();
        num_month = userMap['data']['orderStatistics']['num_month'].toString();
        commission_all= userMap['data']['orderStatistics']['commission_all'].toString();
        commission_wait_today= userMap['data']['orderStatistics']['commission_wait_today'].toString();
        commission_wait_yesterday= userMap['data']['orderStatistics']['commission_wait_yesterday'].toString();
        commission_all_month= userMap['data']['orderStatistics']['commission_all_month'].toString();
        commission_finish_month= userMap['data']['orderStatistics']['commission_all_month'].toString();
        commission_all_lastmonth= userMap['data']['orderStatistics']['commission_all_lastmonth'].toString();
        commission_finish_lastmonth= userMap['data']['orderStatistics']['commission_finish_lastmonth'].toString();
      });
    }
  }
}
